#include "RPGCharacter.h"
#include "RPGShot.h"


RPGShot::RPGShot()
:	// inicializo todos los valores
Sprite(),
m_pkColliding(NULL)
{
	m_bInUse = false;
	SetCheckCollision(false);
}
//----------------------------------------------------------------------
RPGShot::~RPGShot()
{

}
//----------------------------------------------------------------------
// setea el personaje con el cual esta colisionando
void RPGShot::SetCollisionCharacter(RPGCharacter* pkColliding)
{
	m_pkColliding = pkColliding;
}
//----------------------------------------------------------------------
RPGCharacter* RPGShot::GetCollisionCharacter()
{
	return m_pkColliding;
}
//----------------------------------------------------------------------
void RPGShot::Draw()
{
	Sprite::Draw();
}
//----------------------------------------------------------------------
void RPGShot::Update(float fTimeBetweenFrames)
{
	Sprite::Update(fTimeBetweenFrames);
	float x;
	float y;
	g_renderer.GetViewPosition(x,y);
	if(m_bInUse){
		if (abs( GetPosX() - x ) > g_renderer.GetViewPortWidth()/2){
			m_bInUse = false;
		}
	}

	if(m_pkColliding)
	{
		SetCheckCollision(false);
		m_pkColliding->SetEnergy(m_pkColliding->GetEnergy() - 10);
		SetVisible(false);
		m_bInUse=false;
	}

}
//----------------------------------------------------------------------
// chequea colision contra un Entity2D (solamente cajas...)
bool RPGShot::CheckCollision(Sprite *pkSprite)
{
	if (!GetCheckCollision())
		return false;

	// calculo las posiciones de las cajas
	float fColPosX01, fColPosY01;
	float fColPosX02, fColPosY02;

	GetCollisionPos(fColPosX01, fColPosY01);
	pkSprite->GetCollisionPos(fColPosX02, fColPosY02);

	// calculo las extensiones de las cajas
	float fExtX01 = (GetWidth() * GetScaleX()) * 0.5f;
	float fExtY01 = (GetHeight() * GetScaleY()) * 0.5f;
	float fExtX02 = (pkSprite->GetWidth() * pkSprite->GetScaleX()) * 0.5f;
	float fExtY02 = (pkSprite->GetHeight() * pkSprite->GetScaleY()) * 0.5f;

	// calculo la colision
	float fTempX = (pkSprite->GetPosX() + fColPosX02) - (GetPosX() + fColPosX01);
	float fTempY = (pkSprite->GetPosY() + fColPosY02) - (GetPosY() + fColPosY01);

	return (fabs(fTempX) <= (fExtX01 + fExtX02)) && 
			(fabs(fTempY) <= (fExtY01 + fExtY02));
}